package com.ruoyi.system.service.impl;

import java.util.List;
import cn.hutool.core.util.ObjectUtil;
import com.aliyun.oss.common.utils.LogUtils;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.util.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AHousingFollowMapper;
import com.ruoyi.system.domain.AHousingFollow;
import com.ruoyi.system.service.IAHousingFollowService;

/**
 * 我关注房屋信息Service业务层处理
 *
 * @author gf
 * @date 2024-11-23
 */
@Service
public class AHousingFollowServiceImpl implements IAHousingFollowService {
    @Autowired
    private AHousingFollowMapper aHousingFollowMapper;

    /**
     * 查询我关注房屋信息
     *
     * @param id 我关注房屋信息主键
     * @return 我关注房屋信息
     */
    @Override
    public AHousingFollow selectAHousingFollowById(Long id) {
        return aHousingFollowMapper.selectAHousingFollowById(id);
    }

    /**
     * 查询我关注房屋信息列表
     *
     * @param aHousingFollow 我关注房屋信息
     * @return 我关注房屋信息
     */
    @Override
    public List<AHousingFollow> selectAHousingFollowList(AHousingFollow aHousingFollow) {
        return aHousingFollowMapper.selectAHousingFollowList(aHousingFollow);
    }

    /**
     * 新增我关注房屋信息
     *
     * @param aHousingFollow 我关注房屋信息
     * @return 结果
     */
    @Override
    public int insertAHousingFollow(AHousingFollow aHousingFollow) {
        aHousingFollow.setCreateTime(DateUtils.getNowDate());
        return aHousingFollowMapper.insertAHousingFollow(aHousingFollow);
    }

    /**
     * 修改我关注房屋信息
     *
     * @param aHousingFollow 我关注房屋信息
     * @return 结果
     */
    @Override
    public int updateAHousingFollow(AHousingFollow aHousingFollow) {
        aHousingFollow.setUpdateTime(DateUtils.getNowDate());
    return aHousingFollowMapper.updateAHousingFollow(aHousingFollow);
    }

    /**
     * 批量删除我关注房屋信息
     *
     * @param ids 需要删除的我关注房屋信息主键
     * @return 结果
     */
    @Override
    public int deleteAHousingFollowByIds(Long[] ids) {
        return aHousingFollowMapper.deleteAHousingFollowByIds(ids);
    }

    /**
     * 删除我关注房屋信息信息
     *
     * @param id 我关注房屋信息主键
     * @return 结果
     */
    @Override
    public int deleteAHousingFollowById(Long id) {
        return aHousingFollowMapper.deleteAHousingFollowById(id);
    }

    /**
     * 批量逻辑删除我关注房屋信息
     *
     * @param ids 需要删除的我关注房屋信息主键
     * @return 结果
     */
    @Override
    public int logicDeleteAHousingFollowByIds(Long[] ids) {
        return aHousingFollowMapper.logicDeleteAHousingFollowByIds(ids);
    }
    /**
     * 逻辑删除我关注房屋信息信息
     *
     * @param id 我关注房屋信息主键
     * @return 结果
     */
    @Override
    public int logicDeleteAHousingFollowById(Long id) {
        return aHousingFollowMapper.logicDeleteAHousingFollowById(id);
    }

    @Override
    public SumNumVO selectAHousingFollowListSumNum() {
        Long userId = LoginUtils.getUser().getId();
        return aHousingFollowMapper.selectAHousingFollowListSumNum(userId);
    }

    @Override
    public int selectAHousingFollowListAddHousing(AHousingFollow aHousingFollow) {
        Long userId = LoginUtils.getUser().getId();
        AHousingFollow fo = new AHousingFollow();
        fo.setUserId(userId);
        fo.setHousId(aHousingFollow.getHousId());
        List<AHousingFollow> aHousingFollows = aHousingFollowMapper.selectAHousingFollowList(fo);
        if(aHousingFollows.size() == 0){
            aHousingFollow.setUserId(userId);
            aHousingFollow.setCreateTime(DateUtils.getNowDate());
            aHousingFollowMapper.insertAHousingFollow(aHousingFollow);
            return 1;
        }else{
            aHousingFollowMapper.deleteAHousingFollowById(aHousingFollows.get(0).getId());
            return 0;
        }
    }

    @Override
    public int selectAHousingFollowListFollowFlag(AHousingFollow aHousingFollow) {
        aHousingFollow.setUserId(LoginUtils.getUser().getId());
        List<AHousingFollow> aHousingFollows = aHousingFollowMapper.selectAHousingFollowList(aHousingFollow);
        if (aHousingFollows.size() == 0){
            return 0;
        }else{
            return 1;
        }
    }

    @Override
    public int selectAHousingFollowListCancelHousing(AHousingFollow aHousingFollow) {
        Long userId = LoginUtils.getUser().getId();
        AHousingFollow fo = new AHousingFollow();
        fo.setUserId(userId);
        fo.setHousId(aHousingFollow.getHousId());
        List<AHousingFollow> aHousingFollows = aHousingFollowMapper.selectAHousingFollowList(fo);
        if(aHousingFollows.size() == 0){
            new AccessDeniedException("未找到数据！");
        }
        return aHousingFollowMapper.deleteAHousingFollowById(aHousingFollows.get(0).getId());
    }

    @Override
    public List<AHousingBackXcxVO> selectAHousingFollowFollowList(HousingFollowVO vo) {
        vo.setUserId(LoginUtils.getUser().getId());
        PageUtils.startPage();
        return aHousingFollowMapper.selectAHousingFollowFollowList(vo);
    }

}
